//
//  FR_Par_HistoryViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/12/8.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "FR_Par_HistoryViewController.h"
#import "FR_Record_InformationViewController.h"
#import "FR_HistoryRecordCell.h"
#import "FR_HistoryRecord_Model.h"
#import "SY_Activity_Model.h"
#import "SYMessageTool.h"
@interface FR_Par_HistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton *imgBtn;
    
}

@property (nonatomic, strong) UIImageView  *navBackView;
@property (nonatomic, strong) UILabel      *navTitlelabel;
@property (nonatomic, strong) UITableView  *studentsTable;
@property (nonatomic, strong) UIView       *backView;;
@property (nonatomic, strong) UIImageView  *classArrow;


@property (nonatomic, strong) UILabel      *nohisLabel;
@property (nonatomic, copy) NSString *faceuserid;
@property (nonatomic, copy) NSString *studentid;
@property (nonatomic, copy) NSString *studentScid;
@property (nonatomic, copy) NSString *classId;

@property (nonatomic, strong) UITableView *recordTable;
@property (nonatomic, strong) NSMutableArray *recordArr;
@property (nonatomic, strong) NSMutableArray *studentArr;
@property (nonatomic, strong) NSMutableArray *stuNameArr;

@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation FR_Par_HistoryViewController

- (id)init
{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)loadView {
    
    [super loadView];
    
    [self.view setBackgroundColor:colorFromHex(@"#F5F8FA")];
    
   self.navBar.centerImg.hidden = NO;
   self.navBackView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
   self.navBackView.userInteractionEnabled=YES;
   
   self.navTitlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 8,120, 18)];
   self.navTitlelabel.textAlignment = NSTextAlignmentCenter;
   self.navTitlelabel.font = [UIFont boldSystemFontOfSize:18];
   self.navTitlelabel.textColor = colorFromHex(@"3b3b3b");
   self.navTitlelabel.centerX = 60;
   
   [self.navBackView addSubview:self.navTitlelabel];
   
   
   imgBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
   [imgBtn setFrame:CGRectMake(0, 0, 200, 44)];
   imgBtn.hidden = YES;
   [imgBtn addTarget:self action:@selector(chooseStudent) forControlEvents:(UIControlEventTouchUpInside)];
   
   [self.navBackView addSubview:imgBtn];
   
   [self.navBar.centerImg addSubview:self.navBackView];

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self.view addSubview:self.recordTable];
    
    [self getStudentListData];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)chooseStudent {
    
    if (self.studentsTable.hidden == YES) {
        [self.studentsTable setHidden:NO];
        [self.backView setHidden:NO];
        [self.classArrow setImage:[UIImage imageNamed:@"record_head_classchoose_up_"]];
    }else {
        [self.studentsTable setHidden:YES];
        [self.backView setHidden:YES];
        [self.classArrow setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
    }
    
}

- (void)removeClassTable {
    [self.studentsTable setHidden:YES];
    [self.backView setHidden:YES];
    [self.classArrow setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
}


#pragma mark - Request

- (void)getStudentListData {
    
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:USERID forKey:@"parentid"];
    
    [Utils Java_get:@"api/getChildClass" parameters:dict success:^(NSInteger recode, id resposeJson) {
        
        NSDictionary * dict = (NSDictionary *)resposeJson;
        if ([[dict objectForKey:@"re_code"] integerValue]==0) {
            if ([[dict objectForKey:@"re_data"] count]>0) {
                
                self.studentArr = [dict objectForKey:@"re_data"];
                
                self.stuNameArr = [[NSMutableArray alloc]init];
                for (NSDictionary *dic in self.studentArr) {
                    [self.stuNameArr addObject:[dic valueForKey:@"studentname"]];
                    
                }
                
                self.faceuserid = [NSString stringWithFormat:@"%@",[[self.studentArr objectAtIndex:0] objectForKey:@"faceuserid"]];
                self.studentid = [NSString stringWithFormat:@"%@",[[self.studentArr objectAtIndex:0] objectForKey:@"studentid"]];
                self.studentScid = [NSString stringWithFormat:@"%@",[[self.studentArr objectAtIndex:0] objectForKey:@"scid"]];
                NSArray *classArr = [[self.studentArr objectAtIndex:0] objectForKey:@"item"];
                self.classId = [NSString stringWithFormat:@"%@",[[classArr firstObject] objectForKey:@"classid"]];
                
                if (self.studentArr.count > 1) {
                    self.classArrow = [[UIImageView alloc]initWithFrame:CGRectMake(52.5, self.navTitlelabel.bottom+2, 15, 11)];
                    [self.classArrow setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
                    
                    [self.navBackView addSubview:self.classArrow];
                    
                    self->imgBtn.hidden = NO;
                }
                
                
                NSString *titleStr = [self.stuNameArr objectAtIndex:0];
                self.navTitlelabel.text = titleStr;
                [self.navTitlelabel setWidth:[titleStr length]*19 > 152 ? 152 :[titleStr length]*19];
                self.navTitlelabel.centerX = 60;
                
                
                [self.view addSubview:self.backView];
                [self.view addSubview:self.studentsTable];
                
                if (44*self.stuNameArr.count > SCREENHEIGH - 64 - 44) {
                    self.studentsTable.scrollEnabled = YES;
                    [self.studentsTable setHeight:SCREENHEIGH - 64 - 44];
                }else {
                    
                    [self.studentsTable setHeight:44 * self.stuNameArr.count];
                    self.studentsTable.scrollEnabled = NO;
                }
                
                [self.studentsTable setHidden:YES];
                [self.backView setHidden:YES];
                
                [self getRecordListData];
            }
        }
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    
}

- (void)getRecordListData {
    
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:self.studentid forKey:@"studentid"];
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:@"1" forKey:@"pageno"];
    [paramDic setObject:self.classId forKey:@"classid"];
    [paramDic setObject:self.studentScid forKey:@"scid"];
    
    
    [Utils Java_get:@"api/getStuParEventHis" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        self.pageNum = 1;//重置分页
        self.recordArr = [[NSMutableArray alloc]init];
        
        if (arr.count >= 20) {
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
            }
            [self.recordTable.mj_footer endRefreshing];
            self.nohisLabel.hidden = YES;
        }else if (arr.count > 0 && arr.count < 20) {
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
                
            }
            [self.recordTable.mj_footer endRefreshingWithNoMoreData];
            self.nohisLabel.hidden = YES;
            
        } else {
            self.nohisLabel.hidden = NO;
        }
        
        [self.recordTable.mj_header endRefreshing];
        [self.recordTable reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
}

- (void)loadMoreRecord {
    
    self.pageNum += 1;
    
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:self.studentid forKey:@"studentid"];
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:[NSString stringWithFormat:@"%ld",self.pageNum] forKey:@"pageno"];
    [paramDic setObject:self.classId forKey:@"classid"];
    [paramDic setObject:self.studentScid forKey:@"scid"];
    
    [Utils Java_get:@"api/getStuParEventHis" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count > 0) {
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
            }
            
            [self.recordTable.mj_footer endRefreshing];
        }else {
            self.pageNum -= 1;
            [self.recordTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [self.recordTable reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        
        self.pageNum -= 1;
        [self.recordTable.mj_footer endRefreshing];
        [self showHint:error.localizedDescription];

    }];
    
    /*
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:self.faceuserid forKey:@"faceuserid"];
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:[NSString stringWithFormat:@"%ld",self.pageNum] forKey:@"pageno"];
    [paramDic setObject:@"api_student_facelistbystudent" forKey:@"method"];
    
    [Utils get:nil parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count > 0) {
            
            for (NSDictionary *dic in arr) {
                
                FR_HistoryRecord_Model *model = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
                
                [self.recordArr addObject:model];
            }
            
            [self.recordTable.mj_footer endRefreshing];
        }else {
            self.pageNum -= 1;
            [self.recordTable.mj_footer endRefreshing];
            
        }
        
        [self.recordTable reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
     */
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.studentsTable) {
        return self.studentArr.count;
    }else if (tableView == self.recordTable) {
        return [self.recordArr count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.studentsTable) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classIdentifier"];
        
        UILabel *className;
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"classIdentifier"];
            
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, SCREENWIDTH, 0.5)];
            [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
            [cell.contentView addSubview:lineView];
            
            className = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
            [className setFont:[UIFont systemFontOfSize:16]];
            [className setTextColor:colorFromHex(@"#3b3b3b")];
            className.textAlignment = NSTextAlignmentCenter;
            
            [cell.contentView addSubview:className];
            
        }
        
        className.text = [self.stuNameArr objectAtIndex:indexPath.row];
        
        return cell;
        
    }else if (tableView == self.recordTable) {
        
        FR_HistoryRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hisIdentifier"];
        if (cell == nil) {
            cell = [[FR_HistoryRecordCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"hisIdentifier"];
        }
        
        FR_HistoryRecord_Model *model = [self.recordArr objectAtIndex:indexPath.row];
        
        [cell creatCell:model object:1];
        
        return cell;
    }
    
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (tableView == self.studentsTable) {
        
        self.faceuserid = [[self.studentArr objectAtIndex:indexPath.row] objectForKey:@"faceuserid"];
        self.studentid = [[self.studentArr objectAtIndex:indexPath.row] objectForKey:@"studentid"];
        self.studentScid = [[self.studentArr objectAtIndex:indexPath.row] objectForKey:@"scid"];
        
        NSArray *classArr = [[self.studentArr objectAtIndex:indexPath.row] objectForKey:@"item"];
        self.classId = [NSString stringWithFormat:@"%@",[[classArr firstObject] objectForKey:@"classid"]];
        
        NSString *titleStr = [self.stuNameArr objectAtIndex:indexPath.row];
        self.navTitlelabel.text = titleStr;
        [self.navTitlelabel setWidth:[titleStr length]*19 > 152 ? 152 :[titleStr length]*19];
        self.navTitlelabel.centerX = 60;
        
        [self.classArrow setImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
        
        [self.backView setHidden:YES];
        [self.studentsTable setHidden:YES];
        
        [self getRecordListData];
        self.recordTable.scrollsToTop = YES;
        
    }else if (tableView == self.recordTable) {
        
        FR_Record_InformationViewController *infoVC = [[FR_Record_InformationViewController alloc]init];
        FR_HistoryRecord_Model *model = [self.recordArr objectAtIndex:indexPath.row];
        
        infoVC.recordModel = model;
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    
    
}

#pragma mark - Get

- (UITableView *)recordTable {
    
    if (_recordTable == nil) {
        
        _recordTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight) style:(UITableViewStylePlain)];
        _recordTable.delegate = self;
        _recordTable.dataSource = self;
        _recordTable.rowHeight = 58;
        _recordTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _recordTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self getRecordListData];
        }];
        _recordTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self loadMoreRecord];
        }];
        
        self.nohisLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, SCREENWIDTH, 15)];
        [self.nohisLabel setFont:[UIFont systemFontOfSize:14]];
        [self.nohisLabel setText:@"暂无识别记录"];
        [self.nohisLabel setTextColor:[UIColor colorWithHexString:@"#979797"]];
        self.nohisLabel.textAlignment = NSTextAlignmentCenter;
        self.nohisLabel.hidden = YES;
        
        [_recordTable addSubview:self.nohisLabel];
        
    }
    return _recordTable;
}

- (UITableView *)studentsTable {
    
    if (_studentsTable == nil) {
        
        _studentsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, 0) style:(UITableViewStylePlain)];
        _studentsTable.delegate = self;
        _studentsTable.dataSource = self;
        _studentsTable.rowHeight = 44;
        _studentsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _studentsTable;
}

- (UIView *)backView {
    
    if (_backView == nil) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGH)];
        [_backView setBackgroundColor:[UIColor blackColor]];
        _backView.alpha = 0.5;
        
        //[_backView addSubview:self.classesTable];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeClassTable)];
        [_backView addGestureRecognizer:tap];
    }
    return _backView;
}


@end
